﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Foundation
{
    /// <summary>
    /// File Provider.
    /// </summary>
    public static class PathProvider
    {
        /// <summary>
        /// Startup application directory.
        /// </summary>
        public static readonly string BaseDirectory = AppDomain.CurrentDomain.BaseDirectory;

        /// <summary>
        /// Existses the specified file name.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="baseDirectory">The base directory.</param>
        /// <returns></returns>
        public static bool Exists(string fileName, string baseDirectory = null)
        {
            if (baseDirectory == null || !Directory.Exists(baseDirectory))
                baseDirectory = BaseDirectory;

            if (File.Exists(fileName))
                return true;

            var shortFileName = Path.GetFileName(fileName);
            var files = GetFileList(baseDirectory);
            return files.Contains(shortFileName);
        }

        /// <summary>
        /// Gets the file list.
        /// </summary>
        /// <param name="baseDirectory">The base directory.</param>
        /// <returns></returns>
        public static List<string> GetFileList(string baseDirectory = null)
        {
            if (baseDirectory == null)
                baseDirectory = BaseDirectory;

            var files = new List<string>(Directory.GetFiles(baseDirectory));
            var directories = Directory.GetDirectories(baseDirectory);          
            foreach (var directory in directories) 
                files.AddRange(GetFileList(directory));
            return files;
        }

        /// <summary>
        /// Gets the file list by extension.
        /// </summary>
        /// <param name="extension">The extension.</param>
        /// <param name="baseDirectory">The base directory.</param>
        /// <returns></returns>
        public static List<string> GetFileListByExtension(string extension, string baseDirectory = null)
        {
            if (baseDirectory == null)
                baseDirectory = BaseDirectory;

            extension = extension.StartsWith(".") ? extension : string.Format(".{0}", extension);

            var files = new List<string>(Directory.GetFiles(baseDirectory));
            var directories = Directory.GetDirectories(baseDirectory);
            foreach (var directory in directories)
                files.AddRange(GetFileList(directory));
            return files.Where(f => Equals(Path.GetExtension(f), extension)).ToList();
        }

        /// <summary>
        /// Finds the name of the file by.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="baseDirectory">The base directory.</param>
        /// <returns></returns>
        public static string FindFileByName(string fileName, string baseDirectory = null)
        {
            if (baseDirectory == null || !Directory.Exists(baseDirectory))
                baseDirectory = BaseDirectory;
            
            if (File.Exists(fileName))
                return fileName;

            var shortFileName = Path.GetFileName(fileName);
            var files = GetFileList(baseDirectory);
            var path = files.Find(f => Equals(Path.GetFileName(f), shortFileName));
            path = path ?? (Path.IsPathRooted(fileName) ? fileName : Path.Combine(baseDirectory, fileName));
            Directory.CreateDirectory(Path.GetDirectoryName(path) ?? string.Empty);
            return path;
        }
    }
}
